package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.TrainingMapper;
import com.think.teaching.model.Training;
import com.think.teaching.model.TrainingCheckPoint;
import com.think.teaching.service.TrainingService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 实训 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class TrainingServiceImpl extends BaseServiceImpl<TrainingMapper, Training> implements TrainingService {

	@Override
	public Training AddTraining(Training result) {
		if (this.save(result)) {
			return result;
		}
		return null;
	}


	@Override
	public Training UpdateTraining(Training result) {
		if (this.updateById(result)) {
			return result;
		}
		return null;
	}

	@Override
	public Training GetTraining(Integer id) {
		QueryWrapper<Training> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	public IPage<Training> ListTraining(Integer page, Integer count, Boolean withoutBan,Integer id) {
		QueryWrapper<Training> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		if (id>0) {
			queryWrapper.eq("course_id", id);
		}
		IPage<Training> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}

	@Override
	public Training SetTrainingState(Integer id, Integer state) {
		UpdateWrapper<Training> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	@Override
	public List<TrainingCheckPoint> GetCheckPoint(Integer trainingId, Boolean withoutBan) {
		// TODO Auto-generated method stub
		return null;
	}

}
